package com.heima.article.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {
    //计算热点文章

    @Autowired
    private CacheService cacheService;

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Override
    public void HotArticle() {
        //查询五天内的文章
        LocalDateTime date = LocalDateTime.now().minusDays(5);
        List<ApArticle> articleByLast5days = apArticleMapper.findArticleListByLast5days(date);
        if (CollectionUtils.isEmpty(articleByLast5days)){
            return;
        }

        //计算分
         List <HotArticleVo > hotArticleVos=computeScore (articleByLast5days);
        //分组
        if (hotArticleVos == null){
            return;
        }
        groupAndCache(hotArticleVos);

    }

    //分组和缓存
    private void groupAndCache(List<HotArticleVo> hotArticleVos){
        List<List<HotArticleVo>> groupByChannelId = CollectionUtil.groupByField(hotArticleVos, "channelId");
        if (CollectionUtils.isEmpty(groupByChannelId)){
            return;
        }
        for (List<HotArticleVo> list : groupByChannelId) {
            if (CollectionUtils.isEmpty(list)){
                continue;
            }
            Integer channelId = list.get(0).getChannelId();

            sorteAndCache(list, ArticleConstants.LOADTYPE_LOAD_MORE + channelId.toString());
        }
    }

    //计算分，缓存
    private void sorteAndCache(List<HotArticleVo> hotArticleVos,String key){
        if (CollectionUtils.isEmpty(hotArticleVos) || StringUtils.isEmpty(key)){
            return;
        }
        //hotArticleVos = hotArticleVos.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).collect(Collectors.toList());
hotArticleVos=hotArticleVos.stream().sorted(new Comparator<HotArticleVo>() {
    @Override
    public int compare(HotArticleVo o1, HotArticleVo o2) {
        Integer score = o1.getScore();
        Integer score1 = o2.getScore();

        return score - score1;
    }
}).collect(Collectors.toList());
        if (hotArticleVos .size() > 30){

            hotArticleVos.subList(0,30);
        }
        cacheService.set(key, JSON.toJSONString(hotArticleVos));
    }


    //计算得分

    private List<HotArticleVo> computeScore (List <ApArticle > list){
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<HotArticleVo> list1 = new ArrayList<>();
        //遍历
        for (ApArticle apArticle : list) {
            if (apArticle == null){
                continue;
            }

            HotArticleVo vo = compute(apArticle);
            list1.add(vo);
        }
        return list1;
    }
    //计算总热度
    private HotArticleVo compute(ApArticle article){
        if (article == null) {
            return null;

        }
        Integer score = 0;
        Integer comment = article.getComment();
        Integer likes = article.getLikes();
        Integer views = article.getViews();
        Integer collection = article.getCollection();

        if (comment != null) {
            score+=comment *5;
        }
        if (views != null) {
            score += views;
        }
        if (likes != null) {
            score += likes *3;
        }
        if (collection != null) {
            score += collection * 8;
        }
        HotArticleVo vo = new HotArticleVo();
        BeanUtils.copyProperties(article,vo);
        vo.setScore(score);
        return vo;
        }
    }



